## Universidade Federal do Rio Grande do Sul - Instituto de Informatica
## Departamento de Informatica Teorica
## Modelos de Linguagem de Programacao - 2013/1
## Professor: Lucas Schnorr
## Alunos: Felipe Schimidt e Mario Gasparoni Junior
##
##
## 
## Implementacao da classe Cenario seguindo recomendacoes em:
## http://www.pygame.org/docs/
""" Modulo de definicao da classe Cenario """
import random,os
import pygame
from pygame.locals import *
from Player import Player
from Barril import Barril

class Cenario:
	'Definicao do cenario do jogo'

	#dir		
	main_dir = 0

	#screen
	screenWidth = 0
	screenHeight = 0
	screen = 0
	
	#colors
	white = 255,255,255
	blue = 0,128,255
	black = 0,0,0
	
	#background
	backgroundImage = 'background.png'
	background = 0	
	SCREENRECT = 0
	
	#player
	Player = 0

	#personagens
	

	#inimigos

	
	#Barril
	Barril = 0
	
	'construtor'
	def __init__(self, Height, Width):
		self.main_dir = os.path.split(os.path.abspath(__file__))[0] #game dir
		self.screenHeight = Height
		self.screenWidth = Width	
		self.SCREENRECT = Rect(0, 0, Height, Width)
		self.screen = pygame.display.set_mode((Height,Width)) #cria screen que acomodara todos os objetos do jogo
		self.Player = Player() #cria player principal
		self.Barril = Barril()

	def load_image(self,file):
		"loads an image, prepares it for play"
		file = os.path.join(self.main_dir,  file)
		try:
			surface = pygame.image.load(file) #toda imagem eh carregada como surface
		except pygame.error:
			raise SystemExit('Could not load image "%s" %s'%(file, pygame.get_error()))
		return surface.convert() #corrige cores, filtros, etc na surface gerada a partir da imagem

	
	def load_images(self,*files):	    
		imgs = []
		for file in files:
			imgs.append(Cenario.load_image(file))		
		return imgs

	def createBarril(self):
		self.Barril = 0
	
	def createBackground(self):
		bgdtile = self.load_image(Cenario.backgroundImage) #cria uma 'mini' surface a partir da imagem. Essa surface serah replicada sobre a surface do background
		self.background = pygame.Surface(self.SCREENRECT.size) #cria surface principal do bg
		
		for y in range(0,self.SCREENRECT.height,bgdtile.get_height()):
			for x in range(0, self.SCREENRECT.width, bgdtile.get_width()):
				self.background.blit(bgdtile, (x, y)) #desenha imagem(bgdtile) na surface do background, na posicao (x,y)
		self.screen.blit(self.background, (0,0)) #desenha background na screen
		pygame.display.flip() #atualiza surface na tela	

	def wait(self,time):
		pygame.time.wait(time)

	def mainLoop(self):
		while 1:	
			#loop principal, recomendado de acordo com a documentacao Python Basics
			for event in pygame.event.get():				
				#caso evento QUIT seja disparado (ex: alt+f4)
				if event.type == QUIT:
					return				
			
			self.wait(100) #suspende um pouco	


	def startGame(self):
		'Inicia Jogo'
		pygame.init()
		self.mainLoop()
		
		
